﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace WJP.Reflection
{
    /// <summary>
    /// 动态代理句柄
    /// </summary>
    internal class DynamicMethodHandle
    {
        public MethodInfo Method;
        public DynamicMethodDelegate DynamicMethod;
        public bool HasFinalArrayParam;
        public int MethodParamsLength;
        public Type FinalArrayElementType;

        public DynamicMethodHandle(MethodInfo method)
        {
            if (method == null)
                throw new ArgumentNullException("info");
            this.Method = method;
            ParameterInfo[] parameters = method.GetParameters();
            this.MethodParamsLength = parameters.Length;
            if (MethodCaller.IsLastArray(parameters))
            {
                this.HasFinalArrayParam = true;
                this.FinalArrayElementType = parameters[parameters.Length - 1].ParameterType.GetElementType();
            }
            this.DynamicMethod = DynamicMethodHandlerFactory.CreateMethod(method);
        }
    }
}
